from selenium import webdriver
from unittest import TestCase
from ddt import ddt, data, unpack
from LoadData import LoginData
from LoginOperation import LoginOperation


@ddt
class TestLoad(TestCase):
    @data(*LoginData.success_data)
    @unpack
    def testLogin_success(self, username, password, expect):
        # 打开浏览器
        driver = webdriver.Chrome()
        # 打开网址
        driver.get("http://localhost:90/")
        # 窗口最大化
        driver.maximize_window()

        # 登陆调用
        log = LoginOperation()
        log.login(driver, username, password)

        # 获取调用
        result = log.getTitle(driver)

        # 错误截图保存
        if result != expect:
            driver.save_screenshot(username + "-" + password + ".jpg")
        else:
            print("账号：" + username + "，密码：" + password + "，测试通过！")

        # 退出运行
        driver.quit()

        # 断言
        self.assertEqual(expect, result)

    @data(*LoginData.name_error)
    @unpack
    def testLogin_Username_Error(self, username, password, expect):
        # 打开浏览器
        driver = webdriver.Chrome()
        # 打开网址
        driver.get("http://localhost:90/")
        # 窗口最大化
        driver.maximize_window()

        # 登陆调用
        log = LoginOperation()
        log.login(driver, username, password)

        # 获取调用
        result = log.getUsernameErrorResult(driver)

        # 错误截图保存
        if result != expect:
            driver.save_screenshot(username + "-" + password + ".jpg")
        else:
            print("账号：" + username + "，密码：" + password + "，测试通过！")

        # 退出运行
        driver.quit()

        # 断言
        self.assertEqual(expect, result)

    @data(*LoginData.pwd_error)
    @unpack
    def testLogin_Password_Error(self, username, password, expect):
        # 打开浏览器
        driver = webdriver.Chrome()
        # 打开网址
        driver.get("http://localhost:90/")
        # 窗口最大化
        driver.maximize_window()

        # 登陆调用
        log = LoginOperation()
        log.login(driver, username, password)

        # 获取调用
        result = log.getPasswordErrorResult(driver)

        # 错误截图保存
        if result != expect:
            driver.save_screenshot(username + "-" + password + ".jpg")
        else:
            print("账号：" + username + "，密码：" + password + "，测试通过！")

        # 退出运行
        driver.quit()

        # 断言
        self.assertEqual(expect, result)
